﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data.Imaging;
using RayDen.Library.Entity.Scene;

namespace RayDen.MaterialManager.Controls
{
    public partial class EditTextureForm : Form, IEditForm<ImageTextureInfo>
    {

        public string ValueName { get { return this.Value.Name; } set { this.Value.Name = value; } }

        public ImageTextureInfo Value { get; set; }


        protected EditSpectrumForm editSpectrum;

        public enum TextureType
        {
            ConstantColor,
            Image,
            Procedural
        }

        public EditTextureForm()
        {
            InitializeComponent();
        }


        private void EditTextureForm_Load(object sender, EventArgs e)
        {
            if (this.Value == null)
            {
                this.Close();
            }

            BindForm();
        }

        private void BindForm()
        {
            if (this.Value == null)
                return;
            txtName.Text = this.Value.Name;
            lblPath.Text = this.Value.FilePath;
            this.Refresh();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ctlTextureCanvas_Paint(object sender, PaintEventArgs e)
        {
            var txt = Value;
            var path = txt.FilePath;
            if (!File.Exists(path))
            {
                path = ApplicationController.Instance.CurrentDirectory + "\\" + path;
                if (!File.Exists(path))
                {
                    return;
                }
            };
            using (var bmp = new Bitmap(path))
            {
                e.Graphics.DrawImage(bmp, e.ClipRectangle);
            }
            ctlTextureCanvas.Refresh();
        }


        private void btnOpenImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var oldName = this.Value.Name;
                Value = new ImageTextureInfo() { FilePath = openFileDialog1.FileName, Name = oldName };
                BindForm();
            }
        }

        private void ctlTextureCanvas_Click(object sender, EventArgs e)
        {

        }

        private void btnSetupSolid_Click(object sender, EventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.ValueName = this.txtName.Text;
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

    }

}
